package encome.work.dao;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import encome.work.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author xialinrui, fyftless
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable, UserDetails {
    @TableId
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id; //用户id

    @JsonSerialize(using = ToStringSerializer.class)
    private Long stuId; //学号


    private String username;
    private String password;


    private String name; //姓名

    private String phone; //电话号码

    private String wx; //微信号

    private String qq; //qq号
    @JsonSerialize(using = ToStringSerializer.class)
    private String openId;


    private Integer healthStatus; //健康状况

    private Double temp; //当日体温

    private String academy; //书院

    private String college; //学院

    private Integer eduBack; //学历

    private Integer position; //职位

    private Integer isVolunteer; //是否志愿者

    private Integer volunteerTimes; //服务次数

    private String room; //宿舍号

    private String building; //宿舍楼

    private Long instructorId; //导员id

    private String symptom; //症状

    private boolean admin;

    @TableField(exist = false)
    private List<Menu> menus;

    private Integer status;
    @TableField(exist = false)
    private List<Role> roles;
    @TableField(exist = false)
    private List<Permission> permissions;


    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        if (roles != null && roles.size() > 0) {
            roles.forEach(item -> {
                if (StringUtils.isNotEmpty(item.getCode())) {
                    list.add(new SimpleGrantedAuthority("ROLE_" + item.getCode()));
                }
            });
        }
        if (permissions != null && permissions.size() > 0) {
            permissions.forEach(item -> {
                if (StringUtils.isNotEmpty(item.getCode())) {
                    list.add(new SimpleGrantedAuthority(item.getCode()));
                }
            });
        }
        return list;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }


}
